package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.entity.collaborate.StatCollaborateMarketLogin;
import com.tuanzi.loan.business.repository.statistics.custom.StatMarketLoginCustomRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.statistics.login.StatMarketLoginRequest;
import com.tuanzi.loan.vo.response.statistics.login.StatMarketLoginResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.Tables.COMM_PRODUCT_PRDID;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductChannel.COMM_PRODUCT_CHANNEL;
import static com.tuanzi.loan.generated.jooq.stat.tables.StatMarketLogin.STAT_MARKET_LOGIN;

/**
 * @Author: chenweijun
 * @Date: 2019/3/6 17:06
 * @Description:
 */
@Repository
public class StatMarketLoginRepositoryImpl extends JOOQBaseRepository implements StatMarketLoginCustomRepository{

    @Override
    protected TableImpl getTable() {
        return STAT_MARKET_LOGIN;
    }

    @Override
    public Page<StatMarketLoginResponse> list(StatMarketLoginRequest request, Pageable pageable) {

        fields(STAT_MARKET_LOGIN.fields());
        fields(COMM_PRODUCT_CHANNEL.SECONDARY_CHANNEL_GROUP,COMM_PRODUCT_PRDID.PARENT_ID);
        leftJoin(COMM_PRODUCT_CHANNEL, COMM_PRODUCT_CHANNEL.CHANNEL_CODE.cast(String.class).eq(STAT_MARKET_LOGIN.CHANNEL_CODE));
        leftJoin(COMM_PRODUCT_PRDID, STAT_MARKET_LOGIN.PRODUCT_ID.eq(COMM_PRODUCT_PRDID.PRDID.cast(String.class)));
        between(STAT_MARKET_LOGIN.CREATE_DATE, request.getStartDate(), request.getEndDate());

        eq(STAT_MARKET_LOGIN.PRODUCT_ID, request.getProductId());
        eq(STAT_MARKET_LOGIN.CHANNEL_CODE, request.getChannelCode());
        eq(COMM_PRODUCT_CHANNEL.SECONDARY_CHANNEL_GROUP, request.getSecondaryChannelGroup());

        orderBy(STAT_MARKET_LOGIN.CREATE_DATE, Sort.Direction.DESC);

        DecimalFormat format = new DecimalFormat("#.##%");

        return search(pageable, record -> {

            StatMarketLoginResponse response = new StatMarketLoginResponse();
            response.setCreateDate(record.get(STAT_MARKET_LOGIN.CREATE_DATE));
            response.setProductId(record.get(STAT_MARKET_LOGIN.PRODUCT_ID));
            response.setChannelCode(record.get(STAT_MARKET_LOGIN.CHANNEL_CODE));
            response.setSecondaryChannelGroup(record.get(COMM_PRODUCT_CHANNEL.SECONDARY_CHANNEL_GROUP));
            response.setLoginCountA(record.get(STAT_MARKET_LOGIN.LOGIN_COUNT_A));
            response.setLoginCountB(record.get(STAT_MARKET_LOGIN.LOGIN_COUNT_B));
            response.setRegisterCountA(record.get(STAT_MARKET_LOGIN.APP_REGISTER_A));
            response.setRegisterCountB(record.get(STAT_MARKET_LOGIN.APP_REGISTER_B));
            response.setRegisterCountC(record.get(STAT_MARKET_LOGIN.APP_REGISTER_C));
            response.setRegisterCountD(record.get(STAT_MARKET_LOGIN.APP_REGISTER_D));
            response.setParentProductId(record.get(COMM_PRODUCT_PRDID.PARENT_ID));


            String newUserRate = "0.00%";
            String parentProductNewUserRate = "0.00%";
            String beforeClosureNewUserRate = "0.00%";
            String beforeClosureParentNewUserRate = "0.00%";
            if(response.getLoginCountA() != 0){
                newUserRate = format.format(response.getRegisterCountA().doubleValue() / response.getLoginCountA().doubleValue());
            }

            if(response.getLoginCountA() != 0){
                parentProductNewUserRate = format.format(response.getRegisterCountB().doubleValue() / response.getLoginCountA().doubleValue());
            }

            if(response.getLoginCountB() != 0){
                beforeClosureNewUserRate = format.format(response.getRegisterCountC().doubleValue() / response.getLoginCountB().doubleValue());
            }

            if(response.getRegisterCountB() != 0){
                beforeClosureParentNewUserRate = format.format(response.getRegisterCountD().doubleValue() / response.getRegisterCountB().doubleValue());
            }

            response.setNewUserRate(newUserRate);
            response.setParentProductNewUserRate(parentProductNewUserRate);
            response.setBeforeClosureNewUserRate(beforeClosureNewUserRate);
            response.setBeforeClosureParentProductNewUserRate(beforeClosureParentNewUserRate);
            response.setBpUvTotal(record.get(STAT_MARKET_LOGIN.PB_UV_TOTAL));
            response.setBpUvApi(record.get(STAT_MARKET_LOGIN.BP_UV_API));
            response.setBpApiApplyCount(record.get(STAT_MARKET_LOGIN.BP_API_APPLY_COUNT));
            response.setBpApiLendCount(record.get(STAT_MARKET_LOGIN.BP_API_LEND_COUNT));
            response.setBpApiLendAmount(record.get(STAT_MARKET_LOGIN.BP_API_LEND_AMOUNT));

            return response;

        });
    }

    @Override
    public List<StatCollaborateMarketLogin> findAllByCreateDateAndChannelIn(List<Integer> channels, Date date) {

        fields(STAT_MARKET_LOGIN.fields());
        eq(STAT_MARKET_LOGIN.CREATE_DATE, date);
        in(STAT_MARKET_LOGIN.CHANNEL_CODE, channels);

        return search(StatCollaborateMarketLogin.class);
    }
}
